import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mirror/AppStyle.dart';
import 'package:mirror/api/UserApi.dart';
import 'package:mirror/bean/UserBean.dart';
import 'package:mirror/page/LoginPage.dart';
import 'package:mirror/page/home/MainPage.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(seconds: 2)).then((value) async {
      var navigator = Navigator.of(context);
      try {
        UserBean? user = await UserApi.getUser();
        if (user != null &&
            user.password!.isNotEmpty &&
            user.nickName!.isNotEmpty) {
          if ("${user.userPhone}" == 'null' || "${user.userPhone}".isEmpty) {
            await UserApi.login(
                phone: "",
                password: "${user.password}",
                email: "${user.userEmail}");
          } else {
            await UserApi.login(
                phone: "${user.userPhone}",
                password: "${user.password}",
                email: '');
          }
          navigator.pushReplacement(
              MaterialPageRoute(builder: (c) => const MainPage()));
        } else {
          navigator.pushReplacement(
              MaterialPageRoute(builder: (c) => const LoginPage()));
        }
      } catch (e) {
        navigator.pushReplacement(
            MaterialPageRoute(builder: (c) => const LoginPage()));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Constant.mainColor.shade200,
        statusBarIconBrightness: Brightness.light));
    return Scaffold(
      backgroundColor: Constant.mainColor.shade200,
      body: SafeArea(
        child: Center(
          child: TextLiquidFill(
            text: 'Welcome to\n mirror',
            waveDuration: const Duration(seconds: 5),
            waveColor: Colors.yellow,
            textAlign: TextAlign.center,
            boxBackgroundColor: Constant.mainColor.shade200,
            textStyle: const TextStyle(
              fontSize: 30.0,
              fontWeight: FontWeight.bold,
            ),
            loadDuration: Duration(seconds: 2),
          ),
        ),
      ),
    );
  }
}
//  final String imageUrl;
